package com.jsh.erp.config;

//import com.alibaba.cloud.ai.memory.redis.BaseRedisChatMemoryRepository;
//import com.alibaba.cloud.ai.memory.redis.JedisRedisChatMemoryRepository;
import com.jsh.erp.datasource.entities.Tenant;
import com.jsh.erp.datasource.entities.TenantExample;
import com.jsh.erp.datasource.entities.User;
import com.jsh.erp.datasource.entities.UserExample;
import com.jsh.erp.datasource.mappers.TenantMapper;
import com.jsh.erp.datasource.mappers.UserMapper;
import com.jsh.erp.service.RedisService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用于项目启动时的缓存初始化
 * 缓存格式 key = 为租户id_数据类型_数据id, value视具体类型情况而定
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class RedisDataInitializer implements ApplicationRunner {

    @Resource
    private RedisService redisService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private TenantMapper tenantMapper;

    @Override
    @Transactional
    public void run(ApplicationArguments args) throws Exception {
        //设置参数，启动跳过校验
        System.setProperty("isInitProject","true");
        log.info("开始初始化Redis缓存数据...");

        try {
            //清空所有缓存数据
            redisService.safeDeleteAllCache();

            //获取所有租户并按租户分组缓存
            TenantExample tenantExample = new TenantExample();
            TenantExample.Criteria tenantExampleCriteria = tenantExample.createCriteria();
            tenantExampleCriteria.andEnabledEqualTo(true);

            UserExample userExample;
            List<Tenant> tenants = tenantMapper.selectByExample(tenantExample);
            for (Tenant tenant : tenants) {
                userExample = new UserExample();
                UserExample.Criteria criteria = userExample.createCriteria();
                criteria.andTenantIdEqualTo(tenant.getTenantId());
                List<User> users = userMapper.selectByExample(userExample);
                //缓存的格式为
                for (User user : users) {
                    //key: 租户id_user_用户id
                    //value:职务：用户名
                    redisService.storageKeyWithTime(user.getTenantId()+"_user_"+user.getId(),user.getPosition()+":"+user.getUsername(), System.currentTimeMillis());
                }
            }


            //加载其他信息 。todo

            log.info("缓存初始化完毕");
        } catch (Exception e) {
            log.error("Redis数据初始化失败", e);
            throw e;
        }
        //设置参数，之后的校验不跳过
        System.setProperty("isInitProject","false");
    }
//
//    @Bean
//    public BaseRedisChatMemoryRepository redisChatMemoryRepository() {
//        return JedisRedisChatMemoryRepository.builder()
//                .host("localhost")
//                .port(6379)
//                .password("482244")
//                .build();
//    }

}
